package com.example.letgo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import okhttp3.Call;
import okhttp3.Response;


/**
 * Created by cc on 2018/2/27.
 */

public class RegisterActivity extends AppCompatActivity {
    private Button register;
    private EditText nikeName;
    private EditText userName;
    private EditText passWord;
    private ImageView register_back;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        register_back = findViewById(R.id.register_back);

        register_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        onclike();
    }

    private void onclike() {
        register = findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                push();
            }
        });
    }

//  将数据post到服务器

    private void push() {
        nikeName = findViewById(R.id.register_nikename);
        userName = findViewById(R.id.register_username);
        passWord = findViewById(R.id.register_password);

        String nikename = nikeName.getText().toString();
        String username = userName.getText().toString();
        String password = passWord.getText().toString();

        OkGo.post("http://119.29.113.114:8080/gogogo/user/add")
                .params("userNickName",nikename)
                .params("userEmail",username)
                .params("userPassword",password)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                        Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        Log.e("error:", String.valueOf(e));
                        Toast.makeText(RegisterActivity.this, "网络注册，注册失败！", Toast.LENGTH_SHORT).show();
                    }
                });

    }
}

